﻿using Microsoft.AspNetCore.Hosting;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Ocsp;
using SportsPlatform.Authorization.Users;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SportsPlatform.Helpers
{
    public class ExportHelper
    {
        public static string Export<T>(List<T> data, string Name)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/UpLode/" + DateTime.Now.ToString("yyyyMMdd");
            FolderHelper.AddFolder(path);
            path = path + Name + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            IWorkbook workbook = new XSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("Sheet1");

            var headRow = sheet.CreateRow(0);
            PropertyInfo[] props = typeof(User).GetProperties();
            for (var i = 0; i < props.Length; ++i)
            {
                headRow.CreateCell(i).SetCellValue(props[i].Name);
            }
            for (var i = 0; i < data.Count; ++i)
            {
                var row = sheet.CreateRow(i + 1);
                for (var j = 0; j < props.Length; ++j)
                {
                    row.CreateCell(j).SetCellValue(props[j].GetValue(data[i]).ToString());
                }
            }

            var file = File.Create(path);
            workbook.Write(file);
            return path;
        }

        public static string DataTableToExcel(IWebHostEnvironment web, DataTable dataTable, string Name)
        {
            string path = web.WebRootPath + "\\uploadfile\\file\\" + DateTime.Now.ToString("yyyyMMdd");
            FolderHelper.AddFolder(path);
            string fileName = path + "\\" + Name + ".xlsx";
            try
            {
                IWorkbook workbook = new XSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("sheet1");
                sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dataTable.Columns.Count - 1));
                sheet.CreateRow(0).CreateCell(0).SetCellValue(Name);
                ICellStyle titleStyle = workbook.CreateCellStyle();
                IFont titleFont = workbook.CreateFont();
                titleFont.FontHeightInPoints = 18;//设置字体大小
                titleFont.Color = HSSFColor.Black.Index;//设置字体颜色
                titleFont.FontName = "微软雅黑";
                titleStyle.SetFont(titleFont);
                titleStyle.Alignment = HorizontalAlignment.CenterSelection;//居中
                sheet.GetRow(0).GetCell(0).CellStyle = titleStyle;

                ICellStyle textstyle = workbook.CreateCellStyle();
                textstyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;    //下边框线
                textstyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;      //左边框线
                textstyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;     //右边框线
                textstyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;       //上边框线
                textstyle.Alignment = HorizontalAlignment.Center;//居中

                var headRow = sheet.CreateRow(1);
                for (var i = 0; i < dataTable.Columns.Count; ++i)
                {
                    headRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);
                    sheet.AutoSizeColumn(i);//设置自适应列宽
                    headRow.GetCell(i).CellStyle = textstyle;
                }
                for (var i = 0; i < dataTable.Rows.Count; ++i)
                {
                    var row = sheet.CreateRow(i + 2);
                    for (var j = 0; j < dataTable.Columns.Count; ++j)
                    {

                        row.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());
                        row.GetCell(j).CellStyle = textstyle;

                    }
                }
                var file = File.Create(fileName);
                workbook.Write(file);
                return "/uploadfile/file/" + DateTime.Now.ToString("yyyyMMdd") + "/" + Name + ".xlsx";
            }
            catch
            {
                return "";
            }
        }
    }
}
